package com.lbtc.exception;


import com.lbtc.common.ApiRestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@ControllerAdvice
public class GlobalExceptionHandler {

    //日志输出
    private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleException(Exception e) {
        log.error("Default Exception: ", e);
        return ApiRestResponse.error(LbtcMallExceptionEnum.SYSTEM_ERROR);
    }

    @ExceptionHandler(LbtcMallException.class)
    @ResponseBody
    public Object handleImoocMallException(LbtcMallException e) {
        log.error("LbtcMallException: ", e);
        return ApiRestResponse.error(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ApiRestResponse
    handleMethodExceptionArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("MethodArgumentNotValidException", e);
        return handleBindingResult(e.getBindingResult());
    }

    //统一参数校验异常处理具体实现方法
    private ApiRestResponse handleBindingResult(BindingResult result) {
//把异常处理为对外暴露的提示，list存储所有异常信息的拼接
        ArrayList<String> list = new ArrayList<>();
        if (result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            for (ObjectError allError : allErrors) {
                String defaultMessage = allError.getDefaultMessage();
                list.add(defaultMessage);
            }
        }
        if (list.size() == 0) {//由于list已经初始化，也就不可能等于null，因此需要通过长度判断
//如果为空，返回请求参数错误
            return ApiRestResponse.error(LbtcMallExceptionEnum.REQUEST_PARAM_ERROR);
        }
//通常list不为空，返回也是异常处理，但其中的内容是有所指，也就是将list中的内容暴露出去
        return
                ApiRestResponse.error(LbtcMallExceptionEnum.REQUEST_PARAM_ERROR.getCode(), list.toString());

    }
}
